<template>
	<view style="padding: 10px;">
		<u-form>
			<u-form-item label="上报地点"    label-width="150" >
				<u-input v-model="formData.sbdd" disabled="true"  />
			</u-form-item>
			<u-form-item label="上报时间"    label-width="150" >
				<u-input v-model="formData.sbsj" disabled="true"  />
			</u-form-item>
			<u-form-item label="描述"  :border-bottom="false"  label-width="150" >
				<u-input v-model="formData.ms" type="textarea"  border="true"/>
			</u-form-item>
			<u-form-item label="上报图片"  border-bottom="false" label-width="150" >
				<u-upload ref="imgUpload" :name="name"  @on-uploaded="onUploaded" :action="action" @on-remove="onRemove" ></u-upload>
			</u-form-item>
			<u-form-item label="上报人"    label-width="150" >
				<u-input v-model="formData.sbr" />
			</u-form-item>
			<u-form-item label="上报人电话"    label-width="150" >
				<u-input v-model="formData.sbrdh" />
			</u-form-item>
		</u-form>
		<u-button @click="save()" :disabled="submitBtn" type="primary">保存</u-button>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import { mapState} from 'vuex';
	export default{
		computed:mapState(['user', ]),
		data(){
			return {
				submitBtn:false,
				action:this.$http.config.baseURL+'/xssb/upload',		//图片上传地址
				name: 'files',
				fileList:[],
				formData:{
					sbdd:'',
					sbsj:this.$util.getNowDT(),
					ms:'',
					sbr:'',
					sbrdh:'',
					imgPath:'',
					tjr:''
				}
			}
		},
		methods:{
			onRemove(index, lists) {
				console.log('图片已被移除')
				this.fileList = lists;
			},
			onUploaded(lists) {
				console.log('onUploaded...');
				this.fileList = lists;
			},
			save(){
				var that = this;
				let files = [];
				for(let item of this.fileList){	
					if(item.progress==100){
						files = files.concat(item.response.data);
					}
				}
				this.formData.imgPath = files.join(",");
				that.submitBtn = true;
				this.$http.post("/xssb/save",this.formData).then(res => {
					let data = res.data.data;
					if(data){
						that.$refs.uToast.show({
							title: '提交成功',
							type: 'success',
							callback: ()=>{
								let pages = getCurrentPages(); // 当前页面
								let prePage = pages[pages.length - 2]; // 前一个页面
								uni.navigateBack({
								     success: function() {
										
										prePage.onLoad(); // 执行前一个页面的onLoad方法
										 
								     }
								});
							}
							//back: true
						})
					}else{
						this.submitBtn = false;
						this.$refs.uToast.show({
							title: '提交异常,请稍后重试',
							type: 'error'
						})
					}
				}).catch(err => {
					this.submitBtn = false;
					this.$refs.uToast.show({
						title: '提交失败！',
						type: 'error'
					}) 
				})
			}
		},
		onLoad() {
			let locations = this.$auth.getLocationInfo();
			this.formData.sbdd = locations.address;
			this.formData.tjr = this.user.user.id;
		}
	}
</script>

<style>
</style>
